package org.hq.service.sql;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.hq.pojo.GroupsVideo;
import org.hq.pojo.HostWeb;
import org.hq.pojo.VideoMessage;
import org.hq.upvideo.EupVideoTaskType;
import org.hq.upvideo.RedisKey;
import org.hq.upvideo.UpVideoTaskSiHu;
import org.hq.upvideo.VideoTaskEntity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class MysqlManageServiceTest {
    @Autowired
    private MysqlManageService mms;
    @Autowired
    RedisTemplate<String, String> redisString;
    @Test
    void addWebName() {
        boolean b = mms.addWebName("四虎");
        assertTrue(b);
    }

    @Test
    void addHostWeb() {
        HostWeb hostWeb = new HostWeb();
        hostWeb.setPath("https://www.2e010b15.com");
        hostWeb.setM3u8("https://m3u8.93cdn.com");

        mms.addHostWeb("四虎", hostWeb);
    }
    /**
     * 四虎网站 菜单页面解析
     * PARSE_HTML_SIHU_MENU
     */
    @Test
    void parseMenu(){
        VideoTaskEntity entity =new VideoTaskEntity();
        entity.setTaskType(EupVideoTaskType.PARSE_HTML_SIHU_MENU);
        entity.setWebHost("https://www.2e010b15.com");
        entity.setTaskPath("/av/index.html");
        new UpVideoTaskSiHu(entity).run();


        //uvs.submit(entity);
    }
    @Test
    void saveGroupsVideo() {
        GroupsVideo gv = new GroupsVideo();
        gv.setName("熟女人妻");
        gv.setPath("/movie/shunv");
        mms.saveGroupsVideo("电影", gv, MysqlManageService.AGETYPE_ITEM);


    }


    @Test
    void getLimitHostWeb() {
    }

    @Test
    void pageVideoMessage() {
        String key=String.valueOf(RedisKey.ADD_VIDEO_ITEM_SIHU_KEY);
       int pageSize=2000;

       Page<VideoMessage> page = mms.pageVideoMessage(1, pageSize);
        for (int i=2;i<= page.getPages();i++) {
            Set<String> setJson = new HashSet<>();
            for (VideoMessage record : page.getRecords()) {
                VideoTaskEntity entity = new VideoTaskEntity();
                entity.setHtmlweb(record.getHtmlweb());
                String json = JSON.toJSONString(entity);
                setJson.add(json);

            }
            // 存储 videoHtmpPath 路径用于检测数据库此视频已存在无需添加
            redisString.opsForSet().add(key, setJson.toArray(new String[0]));

            page = mms.pageVideoMessage(i, pageSize);


        }
        //最后一页面
        Set<String> setJson = new HashSet<>();
        for (VideoMessage record : page.getRecords()) {
            VideoTaskEntity entity = new VideoTaskEntity();
            entity.setHtmlweb(record.getHtmlweb());
            String json = JSON.toJSONString(entity);
            setJson.add(json);

        }
        // 存储 videoHtmpPath 路径用于检测数据库此视频已存在无需添加
        redisString.opsForSet().add(key, setJson.toArray(new String[0]));

    }
}